var chargingRecord = [];
var formData = undefined;
var totalPage = 0;
var totalCnt = 0;
var lastheaderNameEN = undefined;

//查询
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("chargingRecordThead", chargingRecordTable, search);
	selectDate("startDate");
	selectDate("endDate");
	scrollTableSuspend("scrollBody", "chargingRecordTable", "chargingRecordTable1", "chargingRecordThead", "scrollHeight");
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	$ajaxList('carTeam', getCarTeam);
	search();
});
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function getCarTeam(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#teamID").append("<option value='" + data[i].teamID + "'>" + data[i].teamName + "</option>");
	}
}
function changeEquipmet(){
	var chargingStationID = $("#chargingStationID").val();
	if (chargingStationID) {
		$("#changeEquipment").show();
		$("#equipmentID").empty();
		$ajaxList('equipmentNo', getEquipmentNo, {"chargingStationID": chargingStationID});
	} else {
		$("#changeEquipment").hide();
		$("#equipmentID").empty();
	}
}
function getEquipmentNo(data) {
	var length = data.length;
	$("#equipmentID").append("<option value=''>请选择</option>");
	for (var i = 0; i < length; i++) {
		$("#equipmentID").append("<option value='" + data[i].equipmentID + "'>" + data[i].equipmentName + "</option>");
	}
	if(formData){
		if(formData.equipmentID != "") {
			$("#equipmentID").val(formData.equipmentID);
		}
	}
}

function results(){
	formReset('chargingRecordSearch');
	$("#changeEquipment").hide();
	$("#changeLine").hide();
	setTimeout(search, 100);
}

function changeLine(){
	var teamID = $("#teamID").val();
	if (teamID) {
		$("#changeLine").show();
		$("#lineID").empty();
		$ajaxList('lineName', getLineName, {"teamID": teamID});
	} else {
		$("#changeLine").hide();
		$("#lineID").empty();
	}
}
function getLineName(data) {
	var length = data.length;
	$("#lineID").append("<option value=''>请选择</option>");
	for (var i = 0; i < length; i++) {
		$("#lineID").append("<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
	}
	if(formData){
		if(formData.lineID != "") {
			$("#lineID").val(formData.lineID);
		}
	}
}
function search(headerNameEN){
	if($("#detailID", parent.document).val() == "backFlag"){
		$("#searchType").val("detail");
		$("#detailID", parent.document).val("");
	}
	var startChargingTime = $("#startChargingTime").val();
	var endChargingTime = $("#endChargingTime").val();
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	if (stringToDate(startChargingTime) > stringToDate(endChargingTime)){
		showMessge("开始时间不能大于结束时间！");
	} else {
//		var url = 'searchChargingRecord?searchFlag=';
//		if (!startChargingTime && !endChargingTime && !$("#chargingStationID").val() && 
//				!$("#teamID").val() && !$("#carNumber").val() && !$("#cardNo").val()) {
//			url = url + "init";
//		}
		$ajax('searchChargingRecord', 'chargingRecordSearch', searchChargingRecord);
	}
}
function searchSumChargingRecord(data) {
	sumChargingRecordList = data.sumChargingRecordList || [];
	for(var i=0; i<sumChargingRecordList.length; i++){
		$("#chargingKWHTotal" + i).html('');
		$("#chargingCountTotal" + i).html('');
		$("#chargingCarTotal" + i).html('');
		$("#chargingStationCount" + i).html('');
		$("#equipmentCount" + i).html('');
		$("#chargingKWHTotal" + i).html("" + ThousandsSeparator(floor(sumChargingRecordList[i].chargingKWHTotal, 0))); 
		$("#chargingCountTotal" + i).html(sumChargingRecordList[i].chargingCountTotal);
		$("#chargingCarTotal" + i).html(sumChargingRecordList[i].chargingCarTotal);
		$("#chargingStationCount" + i).html(sumChargingRecordList[i].chargingStationCount);
		$("#equipmentCount" + i).html(sumChargingRecordList[i].equipmentCount);
	}
	$("#recordLoad").hide();
}
function searchChargingRecord(data) {
	$("#recordLoad").show();
	$("#recordLoad").load("popup/loading");
	$ajax('searchSumChargingRecord', 'chargingRecordSearch', searchSumChargingRecord, '', searchSumChargingRecord);
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	if(data.chargingRecord){
		formData = data && data.chargingRecord;
		if(formData.chargingStationID != "") {
			$("#chargingStationID").val(formData.chargingStationID);
			changeEquipmet();
		}
		if(formData.carNumber != "") {
			$("#carNumber").val(formData.carNumber);
		}
		if(formData.cardNo != "") {
			$("#cardNo").val(formData.cardNo);
		}
		if(formData.teamID != "") {
			$("#teamID").val(formData.teamID);
			changeLine();
		}
		if(formData.startChargingTime != "") {
			$("#startChargingTime").val(formData.startChargingTime);
		}
		if(formData.endChargingTime != "") {
			$("#endChargingTime").val(formData.endChargingTime);
		}
		$("#searchType").val("");
	}
	chargingRecord = data && data.chargingRecordList || [];
	sumChargingRecordList = data.sumChargingRecordList || [];
	createTableBody("chargingRecordTbody", chargingRecordTable, chargingRecord);
	$("#chargingKWHTotal").html('');
	$("#chargingCountTotal").html('');
	$("#chargingCarTotal").html('');
	$("#chargingStationCount").html('');
	$("#equipmentCount").html('');
	$("#chargingKWHTotal").html(ThousandsSeparator(floor(sumChargingRecordList[0].chargingKWHTotal, 0)) || '-'); 
	$("#chargingCountTotal").html(sumChargingRecordList[0].chargingCountTotal || '-');
	$("#chargingCarTotal").html(sumChargingRecordList[0].chargingCarTotal || '-');
	$("#chargingStationCount").html(sumChargingRecordList[0].chargingStationCount || '-');
	$("#equipmentCount").html(sumChargingRecordList[0].equipmentCount || '-');
	for (var i = 0, length = chargingRecord.length;i < length; i++){
		$('#chargingStationName' + i).html('<a class="details" onclick="chargingStationName(' + i + ')">'+chargingRecord[i].chargingStationName + '</a>');
		$('#carNumber' + i).html('<a class="details" onclick="pass(\'' + chargingRecord[i].carNumber + '\')">' + chargingRecord[i].carNumber + '</a>');
		if (chargingRecord[i].carFile) {
			$('#carFileFlag' + i).html('<img src="static/images/fileExistBms.png" style="width:28px;height:28px;" />');
		}
		if (chargingRecord[i].chargingProcessFile) {
			$('#chargingProcessFileFlag' + i).html('<img src="static/images/fileExistCcs.png" style="width:28px;height:28px;" />');
		}
	}
}
function chargingStationName(num){
	window.parent.detalisDrop('chargingStation/chargingStationDetail', num, $("#currentPage").val(), "drownDown1", "" ,"chargingStationPage" ,"chargingStation");
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(chargingRecord[num].chargingStationID);
}
function pass(num){
//	nums = "846" + parseInt(num);
	nums = parseInt(num);
	window.parent.passVal(nums, "passValue", 'vehicle/vehicleInfo', 'drownDown3', '', 'carPage', 'vehicleInfo');
	search();
}
function details(index){
	window.parent.detalisSkip('chargingEquipment/chargingRecordDetail',index,$("#currentPage").val());
	$("#detailID", parent.document).val(chargingRecord[index].id);
	$("#equipmentNo", parent.document).val(chargingRecord[index].equipmentNo);
}

//导出
function exportFile(){
	if (chargingRecord.length > 0) {
		var message = "确定导出吗?";
		if(chargingRecord.length > 1000) {
			message = message + "<br>(只允许导出前1000条数据)";
		}
		showMessge(message, "model2");
	} else {
		showMessge("没有数据可导出！");
	}
}
function sure(){
	showMessge("导出中......");
	$ajax('exportChargingRecord?checkFlag=', undefined, exportFileResult);
}
function exportFileResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportChargingRecord?checkFlag=OK');
		showMessge("导出成功，充电记录文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}
// 导入
function importFile() {
	$("#importFileForm").empty();
	$("#importFileForm").append(
		'<div class="form-group" >'+
		'<label for="importFile" class="col-sm-3 control-label">充电记录文件</label>' + 
		'<div class="col-sm-8"><input id="importFile" name="importFile" type="file" class="form-control"/></div>' + 
		'</div>'
	);
	var option = {
		allowedFileExtensions: ['xls', 'xlsx'],
		showUpload: false,
		previewFileIcon: '<img src="static/images/excel.png" style="width:80px;height:80px;" />',
		showErrorPreview: false
	};
	selectFile("importFile", option);
	$("#fileinputModal").modal("show");
}
function confirm() {
	var importFile = $("#importFile").val();
	if (!importFile) {
		$("#importFile").tips({
			side : 2,
			msg : '请选择要导入的充电记录文件',
			bg : '#AE81FF',
			time : 3
		});
		$("#importFile").focus();
	} else {
		showLoading();
		$ajaxSubmit('importFileForm', 'importChargingRecord', importFileResult);
	}
}
function importFileResult(data) {
	setTimeout(hideLoading, 500);
	$("#fileinputModal").modal("hide");
	var result = data && data.result;
	if (result == "Success") {
		var message = "导入成功";
		var count = data && data.count || 0;
		if (count > 0) {
			message = message + ", 其中有" + count + "条重复数据被忽略";
		}
		showMessge(message + "！");
		search();
	} else if (result == "JsonError") {
		showMessge("导入失败，导入的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导入失败，文件不存在 或 文件内没有数据!");
	} else {
		showMessge("导入异常，请重新选择文件 或 联系管理员！");
	}
}
